import java.util.Scanner;

public class BookManagement {
    static Scanner sc;
    static String[][] users;
    static String[][] books;
    static String[][] pubs;
    static String loginUser;

    static {
        sc = new Scanner(System.in);
        users = new String[20][4];
        books = new String[20][5];
        pubs = new String[20][3];
        loginUser = "";
    }


    public static void main(String[] args){
        init();
        index();
    }

    private static void index(){
        while (true){
            System.out.println("欢迎来到龙院图书管理系统！");
            System.out.println("1.登录 2.注册");
            int L = sc.nextInt();
            switch (L){
                case 1:
                    login();
                    break;
                case 2:
                    register();
                    break;
                case 0:
                    System.out.println("退出系统！");
                    System.exit(0);
            }
        }
    }


    private static boolean login(){
        while(true){
            String userName;
            String passWord;
            System.out.println("请输入用户名：");
            userName = sc.next();
            System.out.println("请输入密码：");
            passWord = sc.next();
            if(userName == null || passWord == null){
                System.out.println("用户名或密码不能为空！请重新输入！");
            }else{
                for (int i = 0;i < users.length;i++){
                    if (userName.equals(users[i][1]) && passWord.equals(users[i][2])){
                        System.out.println("登陆成功！");
                        loginUser = userName;
                        homePage();
                    }
                }
                System.out.println("用户名或密码错误！请重新输入！");
            }
        }
    }


    private static void register(){
        System.out.println("请输入所属部门");
        String deptName = sc.next();
        System.out.println("请输入用户名");
        String userName = sc.next();
        System.out.println("请输入密码");
        String passWord = sc.next();

        for (int i=0;i < users.length;i++){
            if (users[i][1] == null){
                users[i][0] = deptName;
                users[i][1] = userName;
                users[i][2] = passWord;
                users[i][3] = "普通用户";
                System.out.println("注册成功");
                break;
            }else {
                System.out.println("注册失败！");
            }
        }
        index();
    }


    private static void homePage(){
        System.out.println("尊敬的" + loginUser + "，欢迎登录龙岩图书管理系统");

        while (true){
            System.out.println("请输入数字进行选择：1 图书管理  2 出版社管理  3 退出登录  4 退出系统");
            int key = sc.nextInt();
            switch (key){
                case 1:
                    bookManage();
                    break;
                    case 2:
                        pubManage();
                        break;
                    case 3:
                        loginUser = null;
                        System.out.println("退出登录成功");
                        index();
                        break;
                    case 4:
                        System.out.println("系统退出成功");
                        System.exit(0);
                }
        }
    }

    private static void bookManage(){
        while (true){
            System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
            int key = sc.nextInt();
            switch (key){
                case 1:
                    addBook();
                    break;
                case 2:
                    delBook();
                    break;
                case 3:
                    updateBook();
                    break;
                case 4:
                    searchBook();
                    break;
                case 5:
                    homePage();
                    break;
            }
        }
    }

    private static void addBook(){
        System.out.println("请输入图书INBS：");
        String isbn = sc.next();
        System.out.println("请输入书名：");
        String bookName = sc.next();
        System.out.println("请输入价格：");
        String bookPrice = sc.next();
        System.out.println("请输入出版社：");
        String pubCom = sc.next();
        System.out.println("请输入作者：");
        String author = sc.next();
        boolean b = insertBook(isbn,bookName,bookPrice,bookPrice,author);
        if (b){
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败，该书已存在！");
        }
    }

    private static boolean insertBook(String isbn, String bookName, String bookPrice, String pubCom, String author){
        boolean b = false;
        int x = -1;
        for (int i = 0;i < books.length;i++){
            if (isbn.equals(books[i][0])){
                x = 1;
                break;
            }
        }
        int i = x;

        if (i == -1){
            int index = getIndexByINBS(isbn);
            books[index][0] = isbn;
            books[index][1] = bookName;
            books[index][2] = bookPrice;
            books[index][3] = pubCom;
            books[index][4] = author;
            b = true;
        }
        return b;
    }

    static int getIndexByINBS(String isbn){
        int index = -1;

        for (int i = 0;i < books.length;i++){
            if (isbn.equals(books[i][0])){
                index = i;
                break;
            }
        }
        return index;
    }

    private static void delBook(){
        int index = -1;
        System.out.println("请输入要删除的书籍名称：");
        String bookname = sc.next();
        for (int i = 0;i < books.length-1;i++){
            if (bookname.equals(books[i][1])){
                index = i;
                break;
            }
        }
        if (index != -1){
            for (int i = index;i<books.length;i++){
                if (i != books.length-1){
                    books[i][0] = books[i+1][0];
                    books[i][1] = books[i+1][1];
                    books[i][2] = books[i+1][2];
                    books[i][3] = books[i+1][3];
                    books[i][4] = books[i+1][4];
                    System.out.println("删除成功！");
                }
            }
        }else {
            System.out.println("未收录此书");
        }
        bookManage();
    }

    private static void updateBook(){
        System.out.println("请输入需要更新的书籍ISBN：");
        String isbn = sc.next();
        int index = getIndexByINBS(isbn);
        if (index != -1){
            System.out.println("请输入新的书名：");
            String bookName = sc.next();
            System.out.println("请输入新的价格：");
            String bookPrice = sc.next();
            System.out.println("请输入新的出版社：");
            String pubCom = sc.next();
            System.out.println("请输入新的作者：");
            String author = sc.next();
            boolean b = updateBook(index,bookName,bookPrice,pubCom,author);
            if (b){
                System.out.println("更新成功！");
            }else {
                System.out.println("更新失败！");
            }
        }else {
            System.out.println("请输入正确的ISBN");
        }
    }

    private static boolean updateBook(int index, String bookName, String bookPrice, String pubCom, String author){
        boolean b = false;
        books[index][0] = bookName;
        books[index][1] = bookPrice;
        books[index][3] = pubCom;
        books[index][4] = author;
        b = true;
        return b;
    }

    private static void searchBook(){
        while (true){
            System.out.println("请输入查询种类：1.isbn 2.书名（模糊） 3.出版社(模糊) 4. 作者 5. 价格范围 6.查询所有 7.返回上一级 ");
            int s = sc.nextInt();
                switch (s){
                    case 1:
                        System.out.println("请输入查询的ISBN号：");
                        String isbn = sc.next();
                        searchBookBYISBN(isbn);
                        break;
                    case 2:
                        System.out.println("请输入查询的书名：");
                        String bookname = sc.next();
                        searchBookBYbookName(bookname);
                        break;
                    case 3:
                        System.out.println("请输入查询的出版社");
                        String pc = sc.next();
                        searchBookBYPC(pc);
                    case 4:
                        System.out.println("请输入查询的作者");
                        String author = sc.next();
                        searchBookBYAuthor(author);
                        break;
                    case 5:
                        System.out.println("请输入最低价格：");
                        double min = sc.nextDouble();
                        System.out.println("请输入最高价格：");
                        double max = sc.nextDouble();
                        searchBookBYPrice(min,max);
                        break;
                    case 6:
                        searchBookAll();
                        break;
                    case 7:
                        bookManage();
                        break;
                    case 0:
                        System.out.println("请输入正确的数字！");
                        break;
                }
        }
    }

    private static void searchBookBYISBN(String isbn){
        for (int i = 0;i <books.length;i++){
            if (isbn.equals(books[i][0])){
                System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
                for (int j = 0;j<books.length;j++){
                    System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[j][0],books[j][1],books[j][2],books[j][3],books[j][4]);
                    break;
                }
            }else {
                System.out.println("请输入正确的ISBN号！");
            }
        }
        searchBook();
    }

    private static void searchBookBYbookName(String bookname) {
        for (int j = 0;j<books.length;j++){
            if ((books[j][0] != null && books[j][1].contains(bookname))){
                System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
                for (int i = 0;i<books.length;i++){
                    System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
                    break;
                }
            }else {
                System.out.println("暂未收录此书！");
            }
        }
        searchBook();
    }

    private static void searchBookBYPC(String pc) {
        for (int i = 0;i<books.length;i++) {
            if (books[i][0] != null && books[i][3].contains(pc)) {
                System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
                for (String[] strings : books) {
                    System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n", strings[0], strings[1], strings[2], strings[3], strings[4]);
                    break;
                }
            } else {
                System.out.println("暂未收录此书！");
                break;
            }
        }
        searchBook();
    }

    private static void searchBookBYPrice(double min, double max) {
        System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
        for(int i = 0;i < books.length;i++){
            if(books[i][0] != null){
                double price=Double.parseDouble(books[i][2]);
                if(min <= price && max >= price && books[i][2] != null){
                    System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
                }
            }
        }
        searchBook();
    }

    private static void searchBookBYAuthor(String author) {
        for (int j = 0;j<books.length;j++){
            if (books[j][0] != null && books[j][4].indexOf(author) != -1){
                System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
                for (int i = 0;i<books.length;i++){
                    System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
                    break;
                }
            }else {
                System.out.println("暂未收录此作者！");
            }
        }
        searchBook();
    }

    private static void searchBookAll() {
        System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");

        for(int i = 0;i < books.length;i ++){
            if (books[i][0] != null ) {
                System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
            }
        }
    }

    private static void pubManage(){
        while (true){
            System.out.println("请输入：1.增加 2.删除 3.更新 4.根据出版社名称查询(模糊) 5.查询所有出版社 6.返回上一级菜单");
            int key = sc.nextInt();
            switch (key){
                case 1:
                    addPpub();
                    break;
                case 2:
                    delPub();
                    break;
                case 3:
                    updatePub();
                    break;
                case 4:
                    searchPubBYName();
                    break;
                case 5:
                    searchPubBYAll();
                    break;
                case 6:
                    homePage();
                case 0:
                    System.out.println("请输入正确的数字！");
                    break;
            }
        }
    }

    private static void addPpub() {
        System.out.println("请输入出版社名称：");
        String pcName = sc.next();
        System.out.println("请输入出版社地址：");
        String pcAddress = sc.next();
        System.out.println("请输入出版社联系人：");
        String pcContact = sc.next();
        for (int i = 0;i<pubs.length;i++){
            if (pcName.equals(pubs[i][0])){
                System.out.println("该出版社已经存在！");
                break;
            }else {
                if (pubs[i][0] == null){
                    pubs[i][0] = pcName;
                    pubs[i][1] = pcAddress;
                    pubs[i][2] = pcContact;
                    System.out.println("添加成功");
                    break;
                }else {
                    System.out.println("发生未知错误，添加失败！");
                }
            }
        }
        pubManage();
    }

    private static void delPub() {
        System.out.println("请输入需要删除的出版社名称：");
        String pnbName = sc.next();
        for (int i = 0;i<books.length;i++){
            if (pnbName.equals(pubs[i][3])){
                System.out.println("书库中存在该出版社的书籍");
                System.out.println("删除失败！");
                break;
            }
        }
        int index = -1;
        for (int i = 0;i < pubs.length-1;i++){
            if (pnbName.equals(pubs[i][0])){
                index = i;
                break;
            }
        }
        if (index != -1){
            for (int i = index;i<pubs.length;i++){
                if (i != pubs.length-1){
                    pubs[i][0] = pubs[i+1][0];
                    pubs[i][1] = pubs[i+1][1];
                    pubs[i][2] = pubs[i+1][2];
                    pubs[i][3] = pubs[i+1][3];
                    pubs[i][4] = pubs[i+1][4];
                    System.out.println("删除成功！");
                }
            }
        }
        pubManage();
    }

    private static void updatePub() {
        System.out.println("请输入需要更新的出版社：");
        String pubName = sc.next();
        for(int i = 0; i <pubs.length;i++){
            if(pubName.equals(pubs[i][0])) {
                System.out.println("请输入新名称：");
                String newpubName = sc.next();
                System.out.println("请输入新地址：");
                String newpubAddress = sc.next();
                System.out.println("请输入新联系人：");
                String newpubContact = sc.next();
                pubs[i][0] = newpubName;
                pubs[i][1] = newpubAddress;
                pubs[i][2] = newpubContact;
                System.out.println("更新成功！");
                pubManage();
                break;
            }else {
                System.out.println("没有此出版社，更新失败！");
                pubManage();
            }
        }
    }


    private static void searchPubBYName() {
        System.out.println("请输入要查询的的的出版社：");
        String pubName = sc.next();
        for (int i = 0;i<pubs.length;i++){
            if (pubs[i][0] != null && pubs[i][1].contains(pubName)){
                System.out.println("出版社名称\t    地址                      \t联系人");
                for(int j = 0;j < pubs.length;j++){
                    if(pubName.equals(pubs[j][0])){
                        System.out.printf("%-10s\t%-20s\t%-4s\n",pubs[j][0],pubs[j][1],pubs[j][2]);
                    }
                    pubManage();
                    break;
                }
            }else {
                System.out.println("不存在此出版社！");
                break;
            }
        }
        pubManage();
    }

    private static void searchPubBYAll() {
        System.out.println("出版社名称\t    地址                      \t联系人");
        for (int i = 0;i<pubs.length;i++){
            if (pubs[i][0] != null){
                System.out.printf("%-10s\t%-20s\t%-4s\n",pubs[i][0],pubs[i][1],pubs[i][2]);
            }
        }
        pubManage();
    }


    private static void init() {
        users[0][0] = "行政部";
        users[0][1] = "admin";
        users[0][2] = "123";
        users[0][3] = "管理员";
        users[1][0] = "市场部";
        users[1][1] = "user01";
        users[1][2] = "123";
        users[1][3] = "普通用户";
        users[2][0] = "软件部";
        users[2][1] = "user02";
        users[2][2] = "123";
        users[2][3] = "普通用户";
        users[3][0] = "硬件部";
        users[3][1] = "user02";
        users[3][2] = "123";
        users[3][3] = "普通用户";
        books[0][0] = "97871010";
        books[0][1] = "史记";
        books[0][2] = "125.0";
        books[0][3] = "中华书局";
        books[0][4] = "司马迁";
        books[1][0] = "97875063";
        books[1][1] = "活着";
        books[1][2] = "20.0";
        books[1][3] = "作家出版社";
        books[1][4] = "余华";
        books[2][0] = "97872290";
        books[2][1] = "三体全集";
        books[2][2] = "168.0";
        books[2][3] = "重庆出版社";
        books[2][4] = "刘慈欣";
        books[3][0] = "97875442";
        books[3][1] = "白夜行";
        books[3][2] = "39.5";
        books[3][3] = "南海出版公司";
        books[3][4] = "[日]东野圭吾";
        pubs[0][0] = "中华书局";
        pubs[0][1] = "北京市王府井大街36号";
        pubs[0][2] = "张三";
        pubs[1][0] = "作家出版社";
        pubs[1][1] = "香港九龙荷李活商业中心8楼";
        pubs[1][2] = "李四";
        pubs[2][0] = "重庆出版社";
        pubs[2][1] = "茶园新区开拓路6号";
        pubs[2][2] = "王五";
        pubs[3][0] = "南海出版公司";
        pubs[3][1] = "海秀中路51-1号星华大厦5楼";
        pubs[3][2] = "周毅";
    }
}
